package com.cqw.service;

import com.cqw.commontest.lottery.RewardConfig;
import com.cqw.commontest.stream.User;
import com.cqw.domain.RewardPool;
import com.cqw.mapper.one.RewardPoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-15 20:40
 * @Description: ${Description}
 */
@Service
@Slf4j
public class RewardPoolService {


    @Resource
    private RewardPoolMapper rewardPoolMapper;


    //总的奖项数据
    public static List<RewardConfig> rewardConfigs = new ArrayList<>();

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public String getInfo() {
        try {
            rewardConfigs.add(new RewardConfig(1L, "小米手机1", 1, 1, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigs.add(new RewardConfig(2L, "小米手机2", 2, 2, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigs.add(new RewardConfig(3L, "小米手机3", 3, 3, simpleDateFormat.parse("2021-04-09 10:00:00")));
            rewardConfigs.add(new RewardConfig(4L, "小米手机4", 4, 2, simpleDateFormat.parse("2021-04-09 13:47:00")));


            long startTime = simpleDateFormat.parse("2021-04-20 10:00:00").getTime();
            long endTime = simpleDateFormat.parse("2021-04-22 10:00:00").getTime();
            long time = endTime - startTime;
            long totalCount = rewardConfigs.stream().map(RewardConfig::getAmount).reduce(Integer::sum).get();

            //时间段
            //long timeF = Math.round(time / totalCount);

            Collections.shuffle(rewardConfigs);

            List<RewardPool> insertList = new ArrayList<>();
            for (RewardConfig rewardConfig : rewardConfigs) {
                for (int i = 0; i < rewardConfig.getAmount(); i++) {
                    RewardPool rewardPool = new RewardPool();
                    rewardPool.setConfigId(rewardConfig.getId());
                    rewardPool.setLeftNum(1);
                    long timeF = Math.round(time / (rewardConfig.getAmount() + 1));
                    rewardPool.setReleaseTime(new Date(startTime + timeF + timeF * i));
                    insertList.add(rewardPool);
                }
            }
            rewardPoolMapper.batchInsert(insertList);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "ok";
    }


    public Integer doLottery() throws Exception {
        rewardConfigs.add(new RewardConfig(1L, "小米手机1", 1, 1, simpleDateFormat.parse("2021-04-09 10:00:00")));
        rewardConfigs.add(new RewardConfig(2L, "小米手机2", 2, 2, simpleDateFormat.parse("2021-04-09 10:00:00")));
        rewardConfigs.add(new RewardConfig(3L, "小米手机3", 3, 3, simpleDateFormat.parse("2021-04-09 10:00:00")));
        rewardConfigs.add(new RewardConfig(4L, "小米手机4", 4, 2, simpleDateFormat.parse("2021-04-09 13:47:00")));

        //User user = new User(1L, "zhangsan", 12, "1255623625@qq.com", "敲代码");

        //抽奖次数
        //Integer lotteryNum = getLotteryNum(rewardConfigs, user.getId());


        return null;
    }


    /**
     * 确认抽奖次数
     *
     * @param
     * @return
     */
    private Integer getLotteryNum(List<RewardConfig> rewardConfigs, Long userId) throws Exception {

        long startTime = simpleDateFormat.parse("2021-04-18 10:00:00").getTime();
        long endTime = simpleDateFormat.parse("2021-04-20 10:00:00").getTime();

        int lotteryNum = 0;

        //抽奖次数限制,每人*天*次抽奖机会
        //long startTime = lotteryActivity.getStartTime().getTime();
        //long endTime = lotteryActivity.getEndTime().getTime();
        //抽奖天数限制,即每次的时间间隔
        //long drawDay = lotteryActivity.getDrawDay() * 24 * 60 * 60 * 1000;
        long drawDay = 1 * 24 * 60 * 60 * 1000;
        //抽奖次数限制
        //Integer drawNum = lotteryActivity.getDrawNum();
        Integer drawNum = 2;
        long now = simpleDateFormat.parse("2021-04-18 10:00:00").getTime();
        //drawDay需要判断你是否为0
        Double ceil = Math.ceil((now - startTime) / drawDay);
        int i = ceil.intValue();
        lotteryNum = i * drawNum;
        if (lotteryNum == 0) lotteryNum = drawNum;

        //加上分享增加抽奖的次数
        int share = 0;
        //todo 加上分享的次数
        //设置的分享次数
        //shareNumF需要判断是否为0
        int shareNumF = 0;
        //设置的增加抽奖次数
        int lotteryNumF = 0;

        int shareNum = Double.valueOf(Math.floor(share / shareNumF)).intValue() * lotteryNumF;

        lotteryNum = lotteryNum + shareNum;

        //减去已经参加的次数
        /*LotteryJoinRecord joinRecord = new LotteryJoinRecord();
        joinRecord.setActivityId(lotteryActivity.getActivityId());
        joinRecord.setJoinerUserId(user.getUserId());
        List<LotteryJoinRecord> joinRecordsDay = lotteryMapper.findJoinRecordByCondition(joinRecord);
        lotteryNum = lotteryNum - (CollectionUtils.isNotEmpty(joinRecordsDay) ? joinRecordsDay.size() : 0);*/

        lotteryNum = lotteryNum - 0;


        return lotteryNum;
    }
}
